package in.rkvsraman.gmailgadgets.simplegadget.client;

import java.util.ArrayList;

import in.rkvsraman.gmailgadgets.simplegadget.shared.FieldVerifier;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.JsArray;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.gadgets.client.Gadget;
import com.google.gwt.gadgets.client.UserPreferences;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.gadgets.client.GadgetFeature;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
@com.google.gwt.gadgets.client.Gadget.ModulePrefs(title = "Clippy Gadget", author = "RKVS Raman", author_email = "rkvsraman@gmail.com")
@com.google.gwt.gadgets.client.Gadget.UseLongManifestName(false)
@com.google.gwt.gadgets.client.Gadget.AllowHtmlQuirksMode(false)
public class GmailSimpleGadget extends Gadget<UserPreferences> {

	ArrayList<String> listB = new ArrayList<String>();
	AgentAnimator animator;
	int agentCounter;
	boolean isStopped;

	@Override
	protected void init(UserPreferences preferences) {
		// TODO Auto-generated method stub

		HorizontalPanel panel = new HorizontalPanel();

		Button start = new Button(
				"<img src='https://fun-gmail-gadgets.googlecode.com/svn/trunk/GmailSimpleGadget/war/gmailsimplegadget/start16.png' alt='Start animating'/>");
		Button stop = new Button("<img src='https://fun-gmail-gadgets.googlecode.com/svn/trunk/GmailSimpleGadget/war/gmailsimplegadget/stop16.png' alt='Stop animating'/>");
		Button next = new Button(
				"<img src='https://fun-gmail-gadgets.googlecode.com/svn/trunk/GmailSimpleGadget/war/gmailsimplegadget/next16.png' alt='Load next character'/>");
		Button about = new Button(
				"<img src='https://fun-gmail-gadgets.googlecode.com/svn/trunk/GmailSimpleGadget/war/gmailsimplegadget/about16.png' alt='About this gadget'/>");

		start.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				// TODO Auto-generated method stub
				startPlaying();

			}
		});

		about.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				// TODO Auto-generated method stub

				Window.alert("Clippy Gadget by RKVS Raman "
						+ "\n\nhttp://fun-gmail-gadgets.googlecode.com/svn"
						+ "/trunk/GmailSimpleGadget/war/gmailsimplegadget/"
						+ "GmailSimpleGadget.gadget.xml");
			}
		});
		stop.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				// TODO Auto-generated method stub
				stopPlaying();

			}
		});
		next.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				// TODO Auto-generated method stub

				loadNext();
			}
		});
		isStopped = false;
		panel.add(start);
		panel.add(stop);
		panel.add(next);
		panel.add(about);
		
		RootPanel.get().add(panel);
		// listB.add("Bonzi");
		listB.add("Clippy");
		listB.add("F1");
		listB.add("Genie");
		listB.add("Genius");
		listB.add("Links");
		listB.add("Merlin");
		listB.add("Peedy");
		listB.add("Rocky");
		listB.add("Rover");

		agentCounter = 0;
		loadAgentandStart();

	}

	protected void loadNext() {
		animator.stopPlaying();

	}

	protected void startPlaying() {
		// TODO Auto-generated method stub
		isStopped = false;
		loadAgentandStart();
	}

	protected void stopPlaying() {
		isStopped = true;
		animator.stopPlaying();

	}

	protected void loadAgentandStart() {
		// TODO Auto-generated method stub

		if (isStopped)
			return;
		loadNewAgent(listB.get(agentCounter));
		agentCounter++;
		agentCounter = agentCounter % listB.size();

	}

	/*
	 * public void loadNewAgent() { String name =
	 * listB.getItemText(listB.getSelectedIndex()); ClippyJS.create(name);
	 * animator = new AgentAnimator(name, this);
	 * Scheduler.get().scheduleFixedDelay(animator, 10000); }
	 */

	public void loadNewAgent(String name) {
		ClippyJS.create(name);
		animator = new AgentAnimator(name, this);
		Scheduler.get().scheduleFixedDelay(animator, 10000);
	}

}
